package com.example.testdemo.list_single_select;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.testdemo.R;

import java.util.List;

/**
 * Created by 颜 on 04.11.011.
 */

public class MyListAdapter extends BaseAdapter {
    private Context context;
    private List<String> list;
    private View tagView;
    public MyListAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Item item;
        if (convertView == null) {
            item = new Item();
            convertView = LayoutInflater.from(context).inflate(R.layout.classify_item, parent, false);
            item.indicator = convertView.findViewById(R.id.indicator);
            item.name = convertView.findViewById(R.id.name);
            convertView.setTag(item);
        }
        item = (Item) convertView.getTag();
        //缁戝畾鏁版嵁
        item.name.setText(list.get(position));
        if (position == 0) {
            item.setSelectTabState(true);
            tagView = convertView;
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tagView != null){
                    Item tagItem = (Item) tagView.getTag();
                    tagItem.setSelectTabState(false);
                }
                Item item = (Item) v.getTag();
                item.setSelectTabState(true);
                tagView = v;
            }
        });
        return convertView;
    }

    class Item {
        private TextView indicator, name;
        public void setSelectTabState(boolean isSelecte){
            if (isSelecte){
                indicator.setBackgroundColor(Color.parseColor("#37a600"));
                name.setBackgroundColor(Color.WHITE);
                name.setTextColor(Color.parseColor("#37a600"));
            }else {
                indicator.setBackgroundColor(Color.parseColor("#F5F5F5"));
                name.setBackgroundColor(Color.parseColor("#F5F5F5"));
                name.setTextColor(Color.parseColor("#3D3D3D"));
            }
        }
    }

}
